package com.dmz.workflow.controller;

import com.dmz.workflow.req.ListQuery;
import com.dmz.workflow.req.MessageProcessReq;
import com.dmz.workflow.req.ProcessInstanceReq;
import com.dmz.workflow.res.FormAdviceConguration;
import com.echronos.commons.Result;
import com.echronos.system.resp.member.MemberDetailsResp;
import com.echronos.workflow.constant.ProcessConstants;
import com.echronos.workflow.res.StartProcessRes;
import com.dmz.workflow.service.FlowableTaskService;
import com.dmz.workflow.service.ProcessInstanceService;
import com.dmz.workflow.vo.ProcessDetailsVo;
import com.dmz.workflow.vo.TaskListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程实例查看和操作
 **/
@Slf4j
@RestController
@RequestMapping("/v1/flowable/processInstance")
public class ProcessInstanceController {

    @Autowired
    private ProcessInstanceService processInstanceService;

    @Autowired
    private FlowableTaskService flowableTaskService;

    /**
     * 根据流程实例id查看详情（查看流程详情）
     */
    @GetMapping(value = "/commentsInfo")
    public Result<ProcessDetailsVo> commentsInfo(@RequestParam String processInstanceId) {
        return Result.build(processInstanceService.getProcessDetail(processInstanceId));
    }

    /**
     * 查看我发起的流程
     **/
    @GetMapping(value = "/listStartedByMe")
    public Result<List<TaskListVo>> listStartedByMe(ListQuery queryVo) {
        return processInstanceService.listStartedByMe(queryVo);
    }

    /**
     * 启动流程实例(发起流程)
     */
    @PostMapping(value = "/start")
    @Transactional(rollbackFor = Exception.class)
    public Result<StartProcessRes> start(@RequestBody ProcessInstanceReq processInstanceReq) {
        processInstanceReq.setProcessInstanceType(ProcessConstants.PROCESS_INSTANCE_COMMON_TYPE);
        return processInstanceService.startProcess(processInstanceReq);
    }

    /**
     * 删除流程实例
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam String processInstanceId, @RequestParam(required = false) boolean cascade,
                            @RequestParam(required = false) String deleteReason) {
        processInstanceService.deleteProcess(processInstanceId, cascade, deleteReason);
        return Result.build();
    }

    /**
     * 抄送列表
     */
    @GetMapping(value = "/listCcToMe")
    public Result<List<TaskListVo>> listCcToMe(ListQuery queryVo) {
        return flowableTaskService.listCcToMe(queryVo);
    }

    /**
     * 通过推送的消息查看审批详情
     */
    @PostMapping(value = "/queryMessageProcessInfo")
    public Result<ProcessDetailsVo> queryMessageProcessInfo(@RequestBody MessageProcessReq messageProcessReq) {
        return Result.build(processInstanceService.getProcessDetail(messageProcessReq.getProcessInstanceId()));
    }

    /**
     * 获取流程相关人员用于建群讨论
     */
    @GetMapping(value = "/queryProcessInvolvedMembers")
    public Result<List<MemberDetailsResp>> queryProcessInvolvedMembers(String processInstanceId) {
        return Result.build(processInstanceService.queryProcessInvolvedMembers(processInstanceId));
    }

    /**
     * 查询form配置
     */
    @GetMapping(value = "/queryFormConfiguration")
    public Result<FormAdviceConguration> queryFormConfiguration(@RequestParam String processInstanceId) {
        return Result.build(processInstanceService.queryFormConfiguration(processInstanceId));
    }
}
